package com.wind.cloud.demo.controller;

import com.wind.cloud.demo.vo.ParamVo;
import com.wind.cloud.demo.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.style.DefaultToStringStyler;
import org.springframework.core.style.StylerUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class ProviderController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(String param) {
        log.info("param: {}", param);
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("WIND-DEMO-PROVIDER");
        for (ServiceInstance serviceInstance : serviceInstances) {
            log.info("/hello, host:" + serviceInstance.getHost() + ", port:" + serviceInstance.getPort() + ", service-id:" + serviceInstance.getServiceId());
        }
        return "hello world";
    }

    @PostMapping(value = "/helloPost")
    public ResultVo hello(ParamVo param) {
        log.info("param: {}", StylerUtils.style(param));
        ResultVo vo = new ResultVo();
        vo.setCode(0);
        vo.setMsg("OK");
        return vo;
    }

}
